package com.dong.config;

import com.dong.pojo.Reader;
import com.dong.pojo.User;
import com.dong.service.ReaderService;
import com.dong.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

//自定义Realm
public class UserRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    @Autowired
    ReaderService readerService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        System.out.println("执行了=>授权doGetAuthorizationInfo");

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();


        //拿到当前登录的对象
        Subject subject = SecurityUtils.getSubject();
        String role = (String) subject.getSession().getAttribute("role");
        if("admin".equals(role)){//管理员
            User currentUser = (User) subject.getPrincipal();//拿到User对象
//        subject.getSession().setAttribute("user",currentUser);
            //设置当前用户的当前权限
            info.addStringPermission(currentUser.getRole());
        }else {//学生
            Reader currentReader = (Reader) subject.getPrincipal();
            info.addStringPermission(currentReader.getRole());
        }
        return info;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        System.out.println("执行了=>认证doGetAuthorizationInfo");

        User user = null;
        Reader reader = null;
        Subject subject = SecurityUtils.getSubject();
        String role = (String) subject.getSession().getAttribute("role");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;

        //通过数据库验证
        if("admin".equals(role)){//管理员
            user = userService.selectUserByUsername(token.getUsername());
            if(user == null){
                return null;//抛出异常 UnknownAccountException
            }
            //密码认证，shiro帮我们做
            return new SimpleAuthenticationInfo(user,user.getPassword(),"");
        }else {//学生
            reader = readerService.selectReaderByUsername(token.getUsername());
            if(reader == null){
                return null;//抛出异常 UnknownAccountException
            }
            //密码认证，shiro帮我们做
            return new SimpleAuthenticationInfo(reader,reader.getPassword(),"");
        }
    }

}
